package com.example.firstapp.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.firstapp.R;
import com.example.firstapp.entity.Domain.Mode;

public class MainActivity extends Activity {
	private Mode mode = Mode.SPATIAL;

	// private Mode mode = null;
	// private UserPreference preference = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mode = ((MobileApplication) getApplication()).getMode();
		if (mode == null)
			((MobileApplication) getApplication()).setMode(Mode.SPATIAL);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		MenuItem actionMode = menu.findItem(R.id.action_mode);
		if (mode != null)
			actionMode.setTitle(mode.toString());
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_mode:
			openModeDialog(item);
			return true;
		case R.id.action_preference:
			showPreference();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void showPreference() {
		Intent intent = new Intent(this, ShowProfileActivity.class);
		startActivity(intent);
	}

	private void openModeDialog(final MenuItem item) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.pick_contextual_factor).setItems(
				R.array.contextual_factors,
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						MobileApplication app = ((MobileApplication) getApplication());
						switch (which) {
						case 0:
							// mode = Domain.Mode.SPATIAL;
							app.setMode(Mode.SPATIAL);
							break;
						case 1:
							// mode = Mode.SOCIAL;
							app.setMode(Mode.SOCIAL);
							break;
						case 2:
							// mode = Mode.PREFERENCE;
							app.setMode(Mode.PREFERENCE);
							break;
						default:
							// mode = Mode.ALL;
							app.setMode(Mode.ALL);
							break;
						}
						item.setTitle(app.getMode().toString());
					}
				});
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public void showRestaurant(View view) {
		Mode mode = ((MobileApplication) getApplication()).getMode();

		if (mode == null)
			return;
		Intent intent = new Intent(this, DisplayRestaurantActivity.class);

		// send data and context information
		// Bundle bundle = new Bundle();
		// bundle.putSerializable("mode", mode);
		// intent.putExtras(bundle);

		startActivity(intent);
	}
}
